const gameWidth = gameDom.clientWidth;

class Pipe extends BaseRectangle {
  constructor(height, top, speed, dom) {
    super(52, height, gameWidth, top, speed, 0, dom);
  }


    //   移除水管元素
  onMove() {
    if (this.left < -this.width) {
        // 移除DOM
        this.dom.remove();
    }
  }
}

function getRandom(min,max) {
    return Math.floor(Math.random() * (max - min) + min);
}

console.log(landTop);

class PipePare {
    constructor(speed) {
        this.spaceHeight = 150;
        this.minHeight = 80;
        this.maxHeight = landTop - this.minHeight - this.spaceHeight;
        const upHeight = getRandom(this.minHeight, this.maxHeight);
        const downHeight = landTop - upHeight - this.spaceHeight;
        const downTop = landTop - downHeight;

        // 创建上水管dom对象
        const upDom = document.createElement("div");
        // 设置样式
        upDom.className = "pipe up";
        const downDom = document.createElement("div");
        // 设置样式
        downDom.className = "pipe down";

        this.upPipe = new Pipe(upHeight, 0, speed, upDom); // 上水管
        this.downPipe = new Pipe(downHeight, downTop, speed, downDom); // 下水管

        gameDom.append(upDom);
        gameDom.append(downDom);
    }

    get useLess() {
        return this.upPipe.left < - this.upPipe.width;
    }

    move(duration) {
        this.upPipe.move(duration);
        this.downPipe.move(duration);
    }
}

// 柱子对
class PipePareProducer {
    constructor(speed) {
        this.pairs = [];
        this.timer = null;
        this.tick = 1500;
        this.speed = speed;
    }

    startProduce() {
        if (this.timer) return;

        this.timer = setInterval(() => {
            this.pairs.push(new PipePare(this.speed));

            for (let i = 0; i < this.pairs.length; i++) {
                const pair = this.pairs[i];

                if (pair.useLess) {
                    this.pairs.splice(i, 1);
                    i--;
                }
            }
        }, this.tick);
    }

    stopProduce() {
        clearInterval(this.timer);
        this.timer = null;
    }
}